﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using TimKen.DataEntity;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类MessageBoard。
    /// </summary>
    public class MessageBoardDAO
    {
        public MessageBoardDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("MessageID", "MessageBoard");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MessageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MessageID", SqlDbType.BigInt)};
            parameters[0].Value = MessageID;

            int result = DbHelperSQL.RunProcedure("UP_MessageBoard_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(MessageBoard model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
						new SqlParameter("@Phone", SqlDbType.NVarChar,20),
					new SqlParameter("@Fax", SqlDbType.NVarChar,20),
					new SqlParameter("@Comments", SqlDbType.NVarChar,1000),
					new SqlParameter("@IP", SqlDbType.NVarChar,20),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Title", SqlDbType.NVarChar,200),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,200),
					new SqlParameter("@Company", SqlDbType.NVarChar,100),
					new SqlParameter("@Address", SqlDbType.NVarChar,100),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@State", SqlDbType.NVarChar,50),
					new SqlParameter("@PostalCode", SqlDbType.NVarChar,10),
                    new SqlParameter("@ReplyContent", SqlDbType.VarChar),
					new SqlParameter("@ReplyDate", SqlDbType.DateTime),
					new SqlParameter("@ReplierID", SqlDbType.VarChar,50),
					new SqlParameter("@IsDisplay", SqlDbType.Bit,1)};
            parameters[0].Value = model.Phone;
            parameters[1].Value = model.Fax;
            parameters[2].Value = model.Comments;
            parameters[3].Value = model.IP;
            parameters[4].Value = model.CreateDate;
            parameters[5].Value = model.Title;
            parameters[6].Value = model.UserName;
            parameters[7].Value = model.Email;
            parameters[8].Value = model.Company;
            parameters[9].Value = model.Address;
            parameters[10].Value = model.City;
            parameters[11].Value = model.State;
            parameters[12].Value = model.PostalCode;
            parameters[13].Value = model.ReplyContent;
            parameters[14].Value = model.ReplyDate;
            parameters[15].Value = model.ReplierID;
            parameters[16].Value = model.IsDisplay;
            DbHelperSQL.RunProcedure("UP_MessageBoard_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(MessageBoard model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MessageID", SqlDbType.BigInt,8),
					new SqlParameter("@Phone", SqlDbType.NVarChar,20),
					new SqlParameter("@Fax", SqlDbType.NVarChar,20),
					new SqlParameter("@Comments", SqlDbType.NVarChar,1000),
					new SqlParameter("@IP", SqlDbType.NVarChar,20),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Title", SqlDbType.NVarChar,200),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,200),
					new SqlParameter("@Company", SqlDbType.NVarChar,100),
					new SqlParameter("@Address", SqlDbType.NVarChar,100),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@State", SqlDbType.NVarChar,50),
					new SqlParameter("@PostalCode", SqlDbType.NVarChar,10),
                    new SqlParameter("@ReplyContent", SqlDbType.VarChar),
					new SqlParameter("@ReplyDate", SqlDbType.DateTime),
					new SqlParameter("@ReplierID", SqlDbType.VarChar,50),
					new SqlParameter("@IsDisplay", SqlDbType.Bit,1)};
            parameters[0].Value = model.MessageID;
            parameters[1].Value = model.Phone;
            parameters[2].Value = model.Fax;
            parameters[3].Value = model.Comments;
            parameters[4].Value = model.IP;
            parameters[5].Value = model.CreateDate;
            parameters[6].Value = model.Title;
            parameters[7].Value = model.UserName;
            parameters[8].Value = model.Email;
            parameters[9].Value = model.Company;
            parameters[10].Value = model.Address;
            parameters[11].Value = model.City;
            parameters[12].Value = model.State;
            parameters[13].Value = model.PostalCode;
            parameters[14].Value = model.ReplyContent;
            parameters[15].Value = model.ReplyDate;
            parameters[16].Value = model.ReplierID;
            parameters[17].Value = model.IsDisplay;

            DbHelperSQL.RunProcedure("UP_MessageBoard_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int MessageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MessageID", SqlDbType.BigInt)};
            parameters[0].Value = MessageID;

            DbHelperSQL.RunProcedure("UP_MessageBoard_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MessageBoard GetModel(long MessageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@MessageID", SqlDbType.BigInt)};
            parameters[0].Value = MessageID;

            MessageBoard model = new MessageBoard();
            DataSet ds = DbHelperSQL.RunProcedure("UP_MessageBoard_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["MessageID"].ToString() != "")
                {
                    model.MessageID = int.Parse(ds.Tables[0].Rows[0]["MessageID"].ToString());
                }
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                model.Comments = ds.Tables[0].Rows[0]["Comments"].ToString();
                model.IP = ds.Tables[0].Rows[0]["IP"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                model.City = ds.Tables[0].Rows[0]["City"].ToString();
                model.State = ds.Tables[0].Rows[0]["State"].ToString();
                model.PostalCode = ds.Tables[0].Rows[0]["PostalCode"].ToString();
                model.ReplyContent=ds.Tables[0].Rows[0]["ReplyContent"].ToString();
				if(ds.Tables[0].Rows[0]["ReplyDate"].ToString()!="")
				{
					model.ReplyDate=DateTime.Parse(ds.Tables[0].Rows[0]["ReplyDate"].ToString());
				}
				model.ReplierID=ds.Tables[0].Rows[0]["ReplierID"].ToString();
                if (ds.Tables[0].Rows[0]["IsDisplay"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsDisplay"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsDisplay"].ToString().ToLower() == "true"))
                    {
                        model.IsDisplay = true;
                    }
                    else
                    {
                        model.IsDisplay = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select MessageID,Phone,Fax,Comments,IP,CreateDate,Title,UserName,Email,Company,Address,City,State,PostalCode,ReplyContent,ReplyDate,ReplierID,IsDisplay ");
            strSql.Append(" FROM MessageBoard ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "MessageBoard";
            parameters[1].Value = "MessageID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  成员方法
    }
}
